package com.whisperly.echotalk.starryb.utils;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.webkit.JavascriptInterface;
import android.webkit.WebChromeClient;
import android.webkit.WebResourceRequest;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

import com.whisperly.echotalk.starryb.R;
import com.whisperly.echotalk.starryb.mutual.JsObject;

import java.net.URISyntaxException;
import java.util.Objects;


public class Mutual extends AppCompatActivity {

    String inputUrl = "https://www.chen88.top";

    static class JsObject {
        private String currentHost;

        @JavascriptInterface
        public String getToken() {
            Log.e("info", "i am in getToken under host:" + currentHost);
            if (checkDomain(currentHost, 1)) //High level of security, trust only chen88.top
            {
                Log.e("info", "allowed to call");
                return "{\"info\":\"allowed to call\"}";
            } else {
                Log.e("info", "not allowed to load this url");
                return "not allowed to call";
            }

        }

        @JavascriptInterface
        public String getUsername() {
            Log.e("info", "i am in under host:" + currentHost);
            if (checkDomain(currentHost, 0)) //Low security level, trust chen88.top and chen99.top
            {
                return "{\"info\":\"i am in under host\"}";
            } else
                return "not allowed to call";
        }

        public void setCurrentHost(String currentHost) {
            this.currentHost = currentHost;
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.mutual);
        AppsManager.addActivity(this);
        TextView viewById = findViewById(R.id.toolbar_title);
        if (getIntent().hasExtra("str2")){
            viewById.setText(getIntent().getStringExtra("str2"));
        }

        final JsObject jsObject = new JsObject();
        WebView webView = (WebView) findViewById(R.id.myWebview);
        webView.getSettings().setJavaScriptEnabled(true);
        webView.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
                String inputUrl = request.getUrl().toString();
                Log.e("info", "override url :" + inputUrl);
                Log.e("info", "set JsObject currentHost :" + view.getUrl());
                jsObject.setCurrentHost(view.getUrl()); //Pass the URL to be loaded by the webview to the Js Object, and take the url from the webview, not the url from the request
                if (checkDomain(inputUrl, 0)) {
                    return false; //The domain name is verified and the request is allowed
                }
                return true; //The domain name verification fails, and the request is terminated
            }
        });
        webView.setWebChromeClient(new WebChromeClient());

        webView.removeJavascriptInterface("searchBoxJavaBridge_");
        webView.removeJavascriptInterface("accessibility");
        webView.removeJavascriptInterface("accessibilityTraversal");

        webView.addJavascriptInterface(jsObject, "myObj");


        if (getIntent().hasExtra("str1")){
            inputUrl = getIntent().getStringExtra("str1");
        }
        if (checkDomain(Objects.requireNonNull(inputUrl), 0)) {
            Log.e("info", "start to loadUrl:" + inputUrl);
            jsObject.setCurrentHost(inputUrl); //Pass the webview URL to be loaded to JsObject
            webView.loadUrl(inputUrl);
        }
    }

    public static boolean checkDomain(String inputUrl, int securityLevel) {
        if (!inputUrl.startsWith("https://")) {
            return false;
        }
        String[] whiteList = new String[]{"chen88.top", "chen99.top"};
        if (securityLevel == 0) //Low security level, where chen88.top and chen99.top are trusted
        {
            whiteList = new String[]{"chen88.top", "chen99.top"};
        } else if (securityLevel == 1)  //High security level, under which only trust is placed chen88.top
        {
            whiteList = new String[]{"chen88.top"};
        }
        java.net.URI url = null;
        try {
            url = new java.net.URI(inputUrl);
        } catch (URISyntaxException e) {
            return false;
        }
        String inputDomain = url.getHost();
        for (String whiteDomain : whiteList) {
            if (inputDomain.endsWith("." + whiteDomain))
                return true;
        }
        return false;
    }

    @Override
    public void onBackPressed() {
        WebView webView = (WebView) findViewById(R.id.myWebview);
        if (webView.canGoBack()) {
            webView.goBack();
        } else {
            super.onBackPressed();
        }
    }

}